<?php

namespace app\index\controller;

use think\Controller;
use think\Request;
use think\Url;
use think\Session;
use think\Cookie;
use think\Db;

class Index extends Controller
{
    public function index($page = 1)
    {
        $count = Db::name('question q,gqx_category c')->where('c.id=q.cid')->where(['c.display'=>1])->count('q.id');
        // return $count;
        $count = ceil($count/10);
        //判断1<=$page<max
        if ($page < 1){
            $page = 1;
        }elseif ($page > $count){
            $page = $count;
        }
        $data = Db::name('question q')->join('gqx_user u','u.id=q.uid')->join('gqx_category c','q.cid=c.id')->join('gqx_user_info i','i.id=u.id')->field('c.*,c.id as cateid,q.*,u.name as uname,i.photo')->where('c.display=1 and c.id=q.cid ')->page($page,10)->order(['q.time'=>'desc'])->select();
        Cookie::get('index_name')?Session::set('index_name',Cookie::get('index_name')):'';
        Cookie::get('index_id')?Session::set('index_id',Cookie::get('index_id')):'';
        
        $namelist = Db::name('category')->select();
        $top = Db::name('question')->order(['likes'=>'desc'])->limit(10)->select();
        return view('index/index',['CSS_PATH'=>config('index_css'),
            'JS_PATH'=>config('index_js'),
            'ICON_PATH'=>config('index_icon'),
            'IMG_PATH'=>config('index_img'),
            'index_name'=>Session::get('index_name'),
            'namelist'=>$namelist,
            'count'=>$count,
            'page'=>$page,
            'index_id'=>Session::get('index_id'),
            'data'=>$data,
            'top'=>$top
        ]);
    }

    //响应搜索提示功能
    public function search($content = '')
    {
        //根据$content开始模糊查询
        $data = Db::name('question')->where('title','like','%'.$content.'%')->select();
        if (!empty($data)){
            return $data;
        }
    }


    public function user($id = '游客')
    {
        return '用户的id:' . $id;
    }

    public function years($year, $mouth)
    {
        return '今年是' . $year . ' ' . $mouth . '月';
    }

    public function names($name)
    {
        return '用户的名字为:' . $name;
    }

    public function url()
    {
        var_dump(Url::build('index/index/names','name=www'));
    }
}
